package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.PreparedStatement;

import dto.AdminDto;

public class AdminDao {

	public String insertAdmin(AdminDto admin) {

		if (admin == null)
			return "Insert failed! Input data is null.";
		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "INSERT INTO admin ( user_id, password) VALUES (?,?)";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, admin.getId());
			preparedStatement.setString(2, admin.getPassword());
			// preparedStatement.setInt(3, admin.getUserId());

			preparedStatement.execute();
			response = "Inserted successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Insert failed! " + e.getMessage();
		}

		return response;
	}

	public String deleteAdmin(String id) {

		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "DELETE FROM admin WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, id);

			preparedStatement.execute();
			response = "Deleted successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Delete failed! " + e.getMessage();
		}

		return response;
	}

	public String updateAdmin(AdminDto admin) {

		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "UPDATE admin SET password = ?, user_user_id = ? WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);

			preparedStatement.setString(1, admin.getPassword());
			preparedStatement.setInt(2, admin.getUserId());
			preparedStatement.setString(3, admin.getId());

			preparedStatement.execute();
			response = "Updated successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Update failed! " + e.getMessage();
		}

		return response;
	}

	public AdminDto getAdminById(String id) {
		AdminDto admin = null;

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM admin WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, id);

			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				admin = new AdminDto();
				admin.setId(resultSet.getString("user_id"));
				admin.setPassword(resultSet.getString("password"));
				admin.setUserId(resultSet.getInt("user_user_id"));
			}

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return admin;
	}

	public AdminDto getAdminByUserId(int userId) {
		AdminDto admin = null;

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM admin WHERE user_user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, userId);

			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				admin = new AdminDto();
				admin.setId(resultSet.getString("user_id"));
				admin.setPassword(resultSet.getString("password"));
				admin.setUserId(resultSet.getInt("user_user_id"));
			}

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return admin;
	}

	public ArrayList<AdminDto> getAllAdmin() {

		ArrayList<AdminDto> listAdmin = new ArrayList<>();

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM admin";

		Statement statement;
		try {
			statement = connection.createStatement();

			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				AdminDto admin = new AdminDto();
				admin.setId(resultSet.getString("user_id"));
				admin.setPassword(resultSet.getString("password"));
				admin.setUserId(resultSet.getInt("user_user_id"));
				listAdmin.add(admin);
			}

			statement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return listAdmin;
	}

}
